<?php

namespace app\api\model;

use think\Model;

class TradeBuy extends Model
{
    protected $name = 'user_order_buy';

    public function addBuys($uid, $mobile, $original_order_ticket, $least_deal_ticket, $unit_price, $remark)
    {
        //写入表
      $aa =    TradeBuy::create([
            'user_id' => $uid,//id
            'user_mobile' => $mobile,//手机号
            'order_type' => 1,//交易类型
            'original_order_ticket' => $original_order_ticket,//原始交易总分
            'order_ticket' => $original_order_ticket,//交易积分
            'least_deal_ticket' => $least_deal_ticket,//最低成交分
            'unit_price' => $unit_price,//单价
            'order_status' => 1,
            'create_by' => $mobile,//创建人
            'create_time' => date('Y-m-d H:i:s'),//创建时间
            'update_by' => $mobile,//更新人
            'update_time' => date('Y-m-d H:i:s'),//更新时间
            'remark' => $remark,//备注
        ]);
      return $aa;
    }
    //更新交易积分字段(卖)
    public function updateTicket1($id, $order_ticket,$update_by,$remark)
    {
        //更新表
        TradeBuy::where(['buy_order_id' => $id])->setDec('order_ticket', $order_ticket);
        TradeBuy::where(['buy_order_id' => $id])->update([
            'update_time' => date('Y-m-d H:i:s'),
            'update_by' => $update_by,
            'remark' => $remark,
        ]);
    }
    public function updateTicket4($id, $order_ticket,$update_by,$remark)
    {
        //更新表
        TradeBuy::where(['buy_order_id' => $id])->setInc('order_ticket', $order_ticket);
        TradeBuy::where(['buy_order_id' => $id])->update([
            'update_time' => date('Y-m-d H:i:s'),
            'update_by' => $update_by,
            'remark' => $remark,
        ]);
    }
    //关联用户表
    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->field('id,avatar,nickname');
    }
}